package cn.openea.perpartner.wxpay.controller;

import cn.openea.perpartner.wxpay.service.WxPayService;
import cn.openea.perpartner.wxpay.vo.PayVo;
import cn.openea.perpartner.wxpay.vo.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @Name: 微信支付统一接口
 * @author: Mr.Yun
 * @Created: 2022/10/13 18:08
 */
@Api(tags = "小程序支付统一接口")
@RestController
@RequestMapping("/api")
public class PayController {

    // 各参数paramType加什么类型说明 https://blog.csdn.net/qq_38972783/article/details/95093090

    @Autowired
    WxPayService wxPayService;

    @ApiOperation("小程序统一下订单-支持零元支付")
    @RequestMapping(value = "/unifiedorder", method = RequestMethod.POST)
    public R createUnifiedorder(@RequestBody @ApiParam(name="统一PayVo对象：仅下订单",
            value="Json格式必填字段：userid, openid, payPrice, tradeDesc, productID, token, authstr，" +
                    "商户订单号 tradeNo 可自定义也可让系统自动生成",required = true)
                                        PayVo payVo) throws Exception {
        return wxPayService.createUnifiedorder(payVo);
    }

    @ApiOperation("小程序统一下订单的回调业务通知")
    @RequestMapping(value = "/ordernotify", method = RequestMethod.POST)
    public String createOrderNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return wxPayService.createOrderNotify(request, response);
    }

    @ApiOperation("查询小程序支付订单信息")
    @RequestMapping(value = "/queryorder", method = RequestMethod.POST)
    public R queryOrderByTradeNo(@RequestBody @ApiParam(name="统一PayVo对象：仅订单号",
            value="Json格式必填字段：userid, openid, tradeNo, token, authstr", required = true)
                                             PayVo payVo) throws Exception {
        return wxPayService.queryOrderByTradeNo(payVo);
    }


    @ApiOperation("查询小程序支付订单是否支付成功")
    @RequestMapping(value = "/queryorderok", method = RequestMethod.POST)
    public R queryOrderByTradeNoIsok(@RequestBody @ApiParam(name="统一PayVo对象：仅订单号",
            value="Json格式必填字段：userid, openid, tradeNo, token, authstr", required = true)
                                         PayVo payVo) throws Exception {
        return wxPayService.queryOrderByTradeNoIsok(payVo);
    }

    @ApiOperation("对指定订单号从商户退款")
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public R refundByTradeNo(@RequestBody @ApiParam(name="统一PayVo对象：仅退款",
            value="Json格式必填字段：userid, openid, tradeNo, payPrice, refundPrice, tradeDesc, token, authstr，" +
                    "退款订单号 refundNo 可自定义也可让系统自动生成", required=true)
                                        PayVo payVo) throws Exception {
        return wxPayService.refundByTradeNo(payVo);
    }

    @ApiOperation("对指定订单号从商户退款的回调业务通知")
    @RequestMapping(value = "/refundnotify", method = RequestMethod.POST)
    public String refundOrderNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return wxPayService.refundOrderNotify(request, response);
    }

    @ApiOperation("创建从商户支付零钱到个人订单")
    @RequestMapping(value = "/paychange", method = RequestMethod.POST)
    public R payToPersonalChange(@RequestBody @ApiParam(name="统一PayVo对象：支付零钱",
            value="Json格式必填字段：userid, openid, payPrice, tradeDesc, token, authstr， " +
                    "支付订单号 tradeNo 可自定义也可让系统自动生成", required = true) PayVo payVo) throws Exception {
        return wxPayService.payToPersonalChange(payVo);
    }

}
